# -*- python -*-
# ex: set syntax=python:

# Copyright 2015 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

import config
import master_site_config

from buildbot.changes.filter import ChangeFilter
from buildbot.schedulers.basic import AnyBranchScheduler
from buildbot.status import mail

from master import gitiles_poller
from master import master_utils
from master import slaves_list
from master.factory import annotator_factory


ActiveMaster = master_site_config.Infra

c = BuildmasterConfig = {}
c['slavePortnum'] = ActiveMaster.slave_port
c['schedulers'] = []
c['builders'] = []
c['change_source'] = []
c['status'] = []

f_annotations = annotator_factory.AnnotatorFactory(ActiveMaster)

####### DATABASE

config.DatabaseSetup(c, require_dbconfig=ActiveMaster.is_production_host)

####### CHANGE SOURCES

INFRA_REPO_URL = 'https://chromium.googlesource.com/infra/infra'
LUCI_GO_REPO_URL = (
    'https://chromium.googlesource.com/external/github.com/luci/luci-go')

c['change_source'].extend([
  gitiles_poller.GitilesPoller(
    repo_url=INFRA_REPO_URL,
    project='infra',
    branches=['master'],
    pollInterval=10,
    revlinktmpl=INFRA_REPO_URL+'/+/%s'),
  gitiles_poller.GitilesPoller(
    repo_url=LUCI_GO_REPO_URL,
    project='luci-go',
    branches=['master', 'go1'],
    pollInterval=10,
    revlinktmpl=LUCI_GO_REPO_URL+'/+/%s'),
])

####### BUILDERS

c['builders'].extend([
  {
    'name': 'infra-continuous-trusty-64',
    'slavebuilddir': 'infra-continuous',
    'factory': f_annotations.BaseFactory('infra/infra_continuous'),
    'auto_reboot' : False,
    'category': '1continuous',
  },
  {
    'name': 'infra-continuous-trusty-32',
    'slavebuilddir': 'infra-continuous',
    'factory': f_annotations.BaseFactory('infra/infra_continuous'),
    'auto_reboot' : False,
    'category': '1continuous',
  },
  {
    'name': 'infra-continuous-precise-64',
    'slavebuilddir': 'infra-continuous',
    'factory': f_annotations.BaseFactory('infra/infra_continuous'),
    'auto_reboot' : False,
    'category': '1continuous',
  },
  {
    'name': 'infra-continuous-precise-32',
    'slavebuilddir': 'infra-continuous',
    'factory': f_annotations.BaseFactory('infra/infra_continuous'),
    'auto_reboot' : False,
    'category': '1continuous',
  },
  {
    'name': 'infra-continuous-mac-10.9-64',
    'slavebuilddir': 'infra-continuous',
    'factory': f_annotations.BaseFactory('infra/infra_continuous'),
    'auto_reboot' : False,
    'category': '1continuous',
  },
  {
    'name': 'infra-continuous-win-32',
    'slavebuilddir': 'infra-continuous',
    'factory': f_annotations.BaseFactory('infra/infra_continuous'),
    'auto_reboot' : False,
    'category': '1continuous',
  },
  {
    'name': 'infra-continuous-win-64',
    'slavebuilddir': 'infra-continuous',
    'factory': f_annotations.BaseFactory('infra/infra_continuous'),
    'auto_reboot' : False,
    'category': '1continuous',
  },
  {
    'name': 'doc-updater',
    'factory': f_annotations.BaseFactory('infra/sphinx_doc_updater'),
    'auto_reboot' : False,
    'category': '1continuous',
  },
  {
    'name': 'luci-go-linux64',
    'factory': f_annotations.BaseFactory('infra/luci_go'),
    'auto_reboot' : False,
    'category': 'luci-go',
  },
  {
    'name': 'luci-go-osx',
    'factory': f_annotations.BaseFactory('infra/luci_go'),
    'auto_reboot' : False,
    'category': 'luci-go',
  },
  {
    'name': 'luci-go-win64',
    'factory': f_annotations.BaseFactory('infra/luci_go'),
    'auto_reboot' : False,
    'category': 'luci-go',
  },
])

####### SCHEDULERS

c['schedulers'].extend([
  AnyBranchScheduler(
      name='infra-scheduler',
      change_filter=ChangeFilter(project=['infra']),
      treeStableTimer=30,
      builderNames=[
          'infra-continuous-trusty-64',
          'infra-continuous-trusty-32',
          'infra-continuous-precise-64',
          'infra-continuous-precise-32',
          'infra-continuous-mac-10.9-64',
          'infra-continuous-win-32',
          'infra-continuous-win-64',
          'doc-updater',
      ]
  ),
  AnyBranchScheduler(
      name='luci-go-scheduler',
      change_filter=ChangeFilter(project=['luci-go']),
      treeStableTimer=1,
      builderNames=[
        'luci-go-linux64',
        'luci-go-osx',
        'luci-go-win64',
      ]
  ),
])

####### BUILDSLAVES

# Associate the slaves to the builders. The configuration is in slaves.cfg.
slaves = slaves_list.SlavesList('slaves.cfg', 'Infra')
for builder in c['builders']:
  builder['slavenames'] = slaves.GetSlavesName(builder=builder['name'])

# The 'slaves' list defines the set of allowable buildslaves. List all the
# slaves registered to a builder. Remove dupes.
c['slaves'] = master_utils.AutoSetupSlaves(
    c['builders'], config.Master.GetBotPassword())

# Make sure everything works together.
master_utils.VerifySetup(c, slaves)

####### STATUS TARGETS

# Adds common status and tools (web status, mail notifier) to this master.
master_utils.AutoSetupMaster(
    c, ActiveMaster, mail_notifier=False, order_console_by_time=True,
    public_html='./public_html',
    templates=['./templates', '../master.chromium/templates']
)

# Send alerts when builders fail.
c['status'].extend([
  mail.MailNotifier(
      fromaddr=ActiveMaster.from_address,
      builders=['doc-updater'],
      subject='doc-updater failed',
      mode='change',
      relayhost=config.Master.smtp,
      sendToInterestedUsers=False,
      extraRecipients=['pgervais@google.com'],
  ),
  mail.MailNotifier(
    fromaddr=ActiveMaster.from_address,
    builders=['luci-go-linux64', 'luci-go-osx', 'luci-go-win64'],
    subject='luci-go CI failed',
    mode='problem',
    relayhost=config.Master.smtp,
    extraRecipients=['swarming-eng@googlegroups.com'],
  ),
])


####### PROJECT IDENTITY

c['projectName'] = ActiveMaster.project_name
c['buildbotURL'] = ActiveMaster.buildbot_url
